package com.bw.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor{




    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 1. 从session中获取当前登录用户
        HttpSession session = request.getSession();

        Object currentUser = session.getAttribute("currentUser");
        if (currentUser == null){
            // 如果没有登录 重定向到 /loginPage.do URL
            response.sendRedirect("/loginPage.do");

        }


        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
